package com.baibei.module.stock.simple;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.baibei.module.stock.R;
import com.baibei.module.stock.basic.BasicStockFragment;
import com.rae.swift.Rx;

/**
 * 简单的股票图
 * Created by ChenRui on 2017/4/17 0017 10:23.
 */
public class SimpleStockFragment extends BasicStockFragment {

    private View mKLineMarkView;
    private TextView mKLineTimeView;
    private TextView mKLineLowView;
    private TextView mKLineOpenView;
    private TextView mKLineCloseView;
    private TextView mKLineHighView;
    private TextView mKLinePercentView;

    public static SimpleStockFragment newInstance(String productId) {
        Bundle args = new Bundle();
        args.putString("productInfo", productId);
        SimpleStockFragment fragment = new SimpleStockFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mKLineMarkView = view.findViewById(R.id.ll_kline_mark_view);
        mKLineTimeView = (TextView) view.findViewById(R.id.tv_marker_kline_time);
        mKLineHighView = (TextView) view.findViewById(R.id.tv_marker_kline_high);
        mKLineLowView = (TextView) view.findViewById(R.id.tv_marker_kline_low);
        mKLineOpenView = (TextView) view.findViewById(R.id.tv_marker_kline_open);
        mKLineCloseView = (TextView) view.findViewById(R.id.tv_marker_kline_close);
        mKLinePercentView = (TextView) view.findViewById(R.id.tv_marker_kline_percent);
    }

    /**
     * 刷新K线mark view
     *
     * @param perclose 上一收价
     * @param time     时间
     * @param high     高
     * @param low      低
     * @param open     开
     * @param close    收
     */
    public void refreshKLineMarkView(float perclose, String time, float high, float low, float open, float close) {
        mKLineMarkView.setVisibility(View.VISIBLE);

        // 涨跌幅 = (当前收价 - 上一个收价) / 上一个收价 * 100%
//        String percentText = "--";
//        float percent = 0;
//        if (perclose > 0) {
//            percent = (close - perclose) / perclose;
//            percentText = Rx.formatPercent(percent);
//        }


        mKLineTimeView.setText(time);
        mKLineHighView.setText(Rx.formatPrice(high));
        mKLineLowView.setText(Rx.formatPrice(low));
        mKLineOpenView.setText(Rx.formatPrice(open));
        mKLineCloseView.setText(Rx.formatPrice(close));
//        mKLinePercentView.setText(percentText);

        // 颜色改变
        invalidateTextColor(mKLineHighView, high, perclose);
        invalidateTextColor(mKLineLowView, low, perclose);
        invalidateTextColor(mKLineOpenView, open, perclose);
        invalidateTextColor(mKLineCloseView, close, perclose);
//        invalidateTextColor(mKLinePercentView, percent, 0);


    }

    /**
     * 比较数值大小并设置相对应的颜色
     *
     * @param view    文本
     * @param current 源
     * @param target  要比较的值
     */
    private void invalidateTextColor(TextView view, float current, float target) {
        int increasingColor = ContextCompat.getColor(getContext(), R.color.quotationIncreasingPrimary); // 涨
        int decreasingColor = ContextCompat.getColor(getContext(), R.color.quotationDecreasingPrimary); // 跌
        int neutralColor = ContextCompat.getColor(getContext(), R.color.textPrimary); // 平

        if (current == target) {
            view.setTextColor(neutralColor);
        } else if (current > target) {
            view.setTextColor(increasingColor);
        } else {
            view.setTextColor(decreasingColor);
        }
    }


    /**
     * 隐藏分时图的MarkView
     */
    public void dismissKLineMarkView() {
        mKLineMarkView.setVisibility(View.GONE);
    }

}
